package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import com.lkm.frame.P;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m_imple.steelyardserver.WeightChangeHelp;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;



public class CMDSubtotalHandle implements ICMDHandle {

    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    public CMDSubtotalHandle() {
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        this.mSteelyardServerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }


    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {
        
        if(cmdEntity.is32Zero()){
            return null;
        }

        if(this.mSteelyardDataModule==null) {
            this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        }
        if(this.mSteelyardServerModule==null) {
            this.mSteelyardServerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        }

        byte[] data= cmdEntity.getData();

        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

        WeightChangeInfo weightChangeInfo= WeightChangeHelp.createByData(steelyardInfo, cmdEntity.getNum(),data);
        if(weightChangeInfo==null){
            P.p("======收到的weightChangeInfo为null");
            return null;
        }


        
        steelyardInfo.addState(SteelyardInfo.RunTimeState_subtotal);

        
        if(weightChangeInfo.getWeight()==0){
            
            weightChangeInfo.setWeight(1);
            weightChangeInfo.setUnitType(2);
            weightChangeInfo.setUnitPrice(weightChangeInfo.getTotalPrice());

        }

        steelyardInfo.addState(SteelyardInfo.RunTimeState_isAddXJ);


        mSteelyardDataModule.weightChange(cmdEntity.getSteelyardMac(),weightChangeInfo);

        
        mSteelyardServerModule.tickOK_block(cmdEntity.getSteelyardMac());
        return null;
    }
}
